package com.isdepci.project.carbon.controller;

import java.util.List;

import com.isdepci.common.utils.StringUtils;
import com.isdepci.project.carbon.utils.ProductCarbonUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.isdepci.framework.aspectj.lang.annotation.Log;
import com.isdepci.framework.aspectj.lang.enums.BusinessType;
import com.isdepci.project.carbon.domain.ProductCarbon;
import com.isdepci.project.carbon.service.IProductCarbonService;
import com.isdepci.framework.web.controller.BaseController;
import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.framework.web.page.TableDataInfo;

/**
 * 产品碳足迹Controller
 *
 * @author isdepci
 * @date 2024-11-07
 */
@RestController
@RequestMapping("/carbon/productCarbon")
public class ProductCarbonController extends BaseController {
    @Autowired
    private IProductCarbonService productCarbonService;
    @Autowired
    private ProductCarbonUtils productCarbonUtils;


    @GetMapping(value = "cal/{id}")
    public AjaxResult calCarbonQuantityById(@PathVariable("id") Long id) {
        return toAjax(productCarbonService.calCarbonQuantityById(id));
    }

    /**
     * 导入其他批次参数
     */
    @GetMapping("/downOther")
    public AjaxResult downOtherProductCarbon(ProductCarbon productCarbon) {
        if(productCarbon.getId()==null||productCarbon.getProductId()==null){
            return AjaxResult.error("参数异常");
        }
        String error = productCarbonUtils.downOtherProductCarbon(productCarbon.getId(),productCarbon.getProductId());
        if(StringUtils.isNotEmpty(error)){
            return AjaxResult.error(error);
        }
        return AjaxResult.success();
    }




    /**
     * 查询产品碳足迹列表
     */
    @PreAuthorize("@ss.hasPermi('carbon:productCarbon:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductCarbon productCarbon) {
        startPage();
        List<ProductCarbon> list = productCarbonService.selectProductCarbonList(productCarbon);
        return getDataTable(list);
    }


    /**
     * 获取产品碳足迹详细信息
     */
    @PreAuthorize("@ss.hasPermi('carbon:productCarbon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productCarbonService.selectProductCarbonById(id));
    }
    @GetMapping(value = "consumables/{id}")
    public AjaxResult getInfoAndConsumables(@PathVariable("id") Long id) {
        return AjaxResult.success(productCarbonService.getInfoAndConsumables(id));
    }

    /**
     * 新增产品碳足迹
     */
    @PreAuthorize("@ss.hasPermi('carbon:productCarbon:add')")
    @Log(title = "产品碳足迹", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductCarbon productCarbon) {
        return (productCarbonService.insertProductCarbon(productCarbon));
    }

    /**
     * 修改产品碳足迹
     */
    @PreAuthorize("@ss.hasPermi('carbon:productCarbon:edit')")
    @Log(title = "产品碳足迹", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductCarbon productCarbon) {
        return toAjax(productCarbonService.updateProductCarbon(productCarbon));
    }

    /**
     * 删除产品碳足迹
     */
    @PreAuthorize("@ss.hasPermi('carbon:productCarbon:remove')")
    @Log(title = "产品碳足迹", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return productCarbonUtils.deleteProductCarbonAllByIds(ids);
    }
}
